//
//  SelectQuestion.cpp
//  QuizMaster
//
//  Created by Jeppe Nielsen on 3/23/15.
//  Copyright (c) 2015 Jeppe Nielsen. All rights reserved.
//

#include "SelectQuestion.hpp"
#include "GameWorld.hpp"
#include "MathHelper.hpp"

using namespace Nano;

void SelectQuestion::Initialize() {
    selectQuestionSystem = Parent().world.GetSystem<SelectQuestionSystem>();
    
    Gui* gui = Parent().gui;
    Vector2 size(6.0f,0.5f);

    currentPlayer = gui->CreateControl(0, "Background", Vector3(0,-3,0), size);
    currentPlayer->GetComponent<Transform>()->Anchor = size*0.5f;
    currentPlayer->GetComponent<Transform>()->Position = Vector3(0,-10,0);
    
    currentPlayerLabel = gui->CreateLabel(currentPlayer, 0, size, Parent().font, "Jeppe", 0.5f);
    currentPlayerLabel->GetComponent<Label>()->HAlignment = Font::Center;
    currentPlayerLabel->GetComponent<Label>()->VAlignment = Font::Middle;
    
    currentPlayer->GetComponent<Material>()->Mask = 1;
    currentPlayerLabel->GetComponent<Material>()->Mask = 1;
    
    showScoresButton = gui->CreateLabelControl(0, "Background", 0, {1,1}, Parent().font, "Scores", 0.3f);
    showScoresButton->GetComponent<Touchable>()->Click += event_handler(this, &SelectQuestion::ShowScoresButtonClicked);
    showScoresButton->GetComponent<Material>()->Mask = 1;
    showScoresButton->Children()[0]->GetComponent<Material>()->Mask = 1;
    showScoresButton->GetComponent<Transform>()->Position = Vector2(0,-10);
}

void SelectQuestion::Update(float dt) {
    if (timerRunning) {
        if (timer>0) {
            timer-=dt;
        } else {
              Parent().CurrentState = "WaitForBuzz";
        }
    }
    
    if (selectQuestionSystem->NoMoreQuestions()) {
        Parent().data.IsGameFinished = true;
        Parent().CurrentState = "ShowScores";
    }
}

void SelectQuestion::Render() {

}

void SelectQuestion::Enabled() {
    timerRunning = false;
    selectQuestionSystem->QuestionSelected += event_handler(this, &SelectQuestion::QuestionSelected);
    
    currentPlayer->GetComponent<Transform>()->Position = Vector3(0,-3,0);
    
    
    
    currentPlayerLabel->GetComponent<Label>()->Text = Parent().data.CurrentPlayer()->Name;
    
    
    currentPlayer->GetComponent<Colorable>()->Color = Parent().data.CurrentPlayer()->color;
    
    currentPlayer->ToFront();
    
    Parent().data.EnabledAllBuzzers();
    
    showScoresButton->GetComponent<Transform>()->Position = Vector2(-5,-3);
    
    
}

void SelectQuestion::Disabled() {
    selectQuestionSystem->QuestionSelected -= event_handler(this, &SelectQuestion::QuestionSelected);
    
    currentPlayer->GetComponent<Transform>()->Position = Vector3(0,-10,0);
    
    showScoresButton->GetComponent<Transform>()->Position = Vector2(0,-10);
}

void SelectQuestion::QuestionSelected(Nano::GameObject* question) {

    if (timerRunning) return;

    question->ToFront();
    
    std::cout<<question->GetComponent<QuestionComponent>()->question->answer<<std::endl;
    
    animation.position.nodes.Clear();
    animation.position.Add(0, 0);
    animation.position.Add(1.0f, question->GetComponent<Transform>()->WorldInverse.GetValue()->TransformPosition(0));
    
    animation.scale.nodes.Clear();
    animation.scale.Add(0, 1);
    animation.scale.Add(1.0f, 3.5f);
    
    animation.rotation.nodes.Clear();
    animation.rotation.Add(0, 0);
    animation.rotation.Add(1.0f, Vector3(0,ToRadians(180),0));
    animation.positionPlayMode = TransformAnimationPlayMode::Clamp;
    
    if (!question->GetComponent<TransformAnimator>()) {
        question->AddComponent<TransformAnimator>();
    }
    
    question->GetComponent<TransformAnimator>()->Play(&animation);
    
    Parent().data.CurrentQuestion = question;
    timer = 1.0f;
    timerRunning = true;
    
    showScoresButton->GetComponent<Transform>()->Position = Vector2(0,-10);
}

void SelectQuestion::ShowScoresButtonClicked(Nano::TouchData d) {
    Parent().CurrentState = "ShowScores";
}
